package com.ys.chatserver.redis;

import com.ys.chatserver.BaseConf;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisManager {
    private static volatile JedisPool jedisPool;

    private RedisManager() {
    }

    private static JedisPool getJedisPoolInstance() {
        if (jedisPool == null) {
            synchronized (RedisManager.class) {
                if (jedisPool == null) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(64);
                    poolConfig.setMaxIdle(32);
                    if (jedisPool == null) {
                        jedisPool = new JedisPool(poolConfig, BaseConf.getInstance().getREDIS_SERVER_IP(),
                                Integer.parseInt(BaseConf.getInstance().getREDIS_SERVER_PORT()),
                                BaseConf.getInstance().getREDIS_LOGIN_USERNAME(),
                                BaseConf.getInstance().getREDIS_LOGIN_PASSWORD());
                    }
                }
            }
        }
        return jedisPool;
    }

    public static Jedis getJedis() {
        return getJedisPoolInstance().getResource();
    }
}